﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls.Primitives;
using System.Windows.Media;

namespace SharpSoft.WPF.Controls
{
    /// <summary>
    /// 矩形设计器
    /// </summary>
    /// <remarks>
    /// 等边梯形的一条平行边的中心在原点上，另一条平行边的中心在原点+方向的点上
    /// </remarks>
    public class RectangleGeoDesigner : GeoDesignerBase
    {
        public RectangleGeoDesigner()
        {
            handler4 = new HandlerThumb() { Shape = HandlerThumbShape.Rhombus };
            this.Handlers.Add(handler4);
            this.Direction = new Vector(100, 100);
            this.Width = 100;

            handler4.DragDelta += Handler4_DragDelta;
        }


        #region 控制点拖动处理 

        private void Handler4_DragDelta(object sender, DragDeltaEventArgs e)
        {
            e.Handled = true;
            var v = this.Direction.Rotate(-Math.PI / 2);//旋转90°得到到宽度控制点的向量
            v.Normalize();
            var v1 = (v * this.Width);
            var change = new Vector(e.HorizontalChange, e.VerticalChange);//拖动的向量
            if (v1.Length == 0)
            {
                this.Width = change.Length;
            }
            else
            {
                var prj = v1.Projection(change);//拖动的向量在原始向量上的映射
                this.Width = (v1 + prj).Length;
            }

        }

        #endregion


        private readonly HandlerThumb handler4;//Width的控制点 


        protected override void OnPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            base.OnPropertyChanged(e);
            if (e.Property == WidthProperty)
            {
                rePositionHandlers();
            }
        }

        protected override void rePositionHandlers()
        {
            var op = this.OriginalPoint;
            var target = op + this.Direction;
            var v = this.Direction.Rotate(-Math.PI / 2);//旋转90°得到到宽度控制点的向量
            v.Normalize();

            handler4.Center = this.OriginalPoint + (v * this.Width);

            var size = new Size(this.Direction.Length, this.Width);
            var rotate = new RotateTransform(Vector.AngleBetween(new Vector(1, 0), this.Direction), op.X, op.Y);
            this.pathGeometry.Clear();
            RectangleGeometry rect = new RectangleGeometry(new Rect(op, size), 0, 0, rotate);
            PathFigure pf = new PathFigure();
            this.pathGeometry.AddGeometry(rect);
            base.rePositionHandlers();
        }

        #region 个性
        /// <summary>
        /// 矩形宽度
        /// </summary>
        public double Width
        {
            get { return (double)GetValue(WidthProperty); }
            set { SetValue(WidthProperty, value); }
        }

        public static readonly DependencyProperty WidthProperty =
            DependencyProperty.Register("Width", typeof(double), typeof(RectangleGeoDesigner), new PropertyMetadata(default(double)));



        #endregion

    }
}
